package com.huan;

import org.apache.zookeeper.*;

import java.io.IOException;

public class DistributeServer {


    public static void main(String[] args) throws Exception {

        DistributeServer server = new DistributeServer();

        //TODO 1 注册节点
        server.getConnect();
        //TODO 2 连接注册服务器信息(在第一台注册)
        server.registServer( args[0] );
        //TODO 3 启动业务功能
        server.business();

    }
    //TODO 3 启动业务功能
    private void business() throws InterruptedException {
        Thread.sleep( Long.MAX_VALUE );
    }
    //TODO 2 连接注册服务器信息(在第一台注册)
    private void registServer(String hostname) throws KeeperException, InterruptedException {

        String path = zkClient.create( "/servers/server", hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL );
        System.out.println( path + "on line" );
    }


    //TODO 注册节点
    private String connectString = "huan01:2181,huan02:2181,huan03:2181";
    private int sessionTimeout = 20000;
    ZooKeeper zkClient;
    
    private void getConnect() throws IOException {
        zkClient = new ZooKeeper( connectString, sessionTimeout, new Watcher() {


            @Override
            public void process(WatchedEvent event) {

            }
        } );
    }
}
